<?php
include_once(MAIN_PATH.'config/global_vars.php');
include_once(MAIN_PATH.'lib/FreeFunctions.php');
class MainController extends EngineSupervisor
{
	protected $view;
	
	public function __construct()
	{
		parent:: __construct();	
	}
	
	public function render()
	{
//	pre($_SESSION);
		if(isset($_GET['mod']) && $_GET['mod']!="" && isset($_GET['view'])&&
		$_GET['view'] != "" && isset($_GET['act']) && $_GET['act']!="")
			{
					$view = $_GET['view'];
					$act = $_GET['act'];
					$mod = $_GET['mod'];
			}
			else
			{
					$view = VIEW;
					$act = ACT;
					$mod = 'back';
			}
			
			if(isset($_GET['mod']) && $_GET['mod'] == "ajax")
			{
				$className = $mod . '_' . $view;
				$object = new $className();
				$object -> $act();
			}
			else
			{
				
				$className = $view . '_controller_' . $view;
				$object = new $className();
				$object -> $act();
				
				$object -> view -> assign('url', SITE_ADDRESS);
				$object -> view -> assign('address', MAIN_ADDRESS);
				
				if ($object -> template === '') 
				{
					$object-> view -> assign('template',MAIN_PATH . 'modules/' . $view . '/views/' . $view . '_' . $act . '.tpl');
				} 
				else 
				{
					$object->view->assign('template',MAIN_PATH . 'modules/' . $view . '/views/' . $view . '_' . $object->template);
				}
				
				if(!isset($object -> submenu) || $object -> submenu == null)
				{
					$object -> view -> assign('submenu',MAIN_PATH . 'modules/' . $view . '/views/' . $view . '_submenu.tpl');
				}
				else
				{
					$object -> view -> assign('template',MAIN_PATH . 'modules/' . $object -> submenu .'.tpl');
				}
				
				if(isset($_SESSION[PREFIX]['message']))
				{
					$object -> view ->assign('message',$_SESSION[PREFIX]['message']);
					unset($_SESSION[PREFIX]['message']);
				}
				
				if(isset($_SESSION[PREFIX]['filtr']))
				{
					$object -> view ->assign('filtr',$_SESSION[PREFIX]['filtr']);
					unset($_SESSION[PREFIX]['filtr']);
				}
				
				if(isset($_SESSION[PREFIX]['moderator']['login']))
				{
					$object -> view->assign('url',SITE_ADDRESS);
					$user = $_SESSION[PREFIX]['moderator']['login'];
					$object -> view -> assign('log',true);
					$object -> view -> assign('user',$user);
					$object -> view -> assign('view',$view);
				}
				if(isset($_SESSION['logout']['message']))
				{
					$object -> view ->assign('message',$_SESSION['logout']['message']);
					unset($_SESSION['logout']['message']);
				}
				
				if(!isset($object -> skeleton) || $object -> skeleton == null)
				{
					$object-> view -> display(MAIN_PATH . 'public/views/skeleton.tpl');
				}
				else
				{
					$object-> view -> display(MAIN_PATH . 'public/views/'.$object -> skeleton);
				}
			}
			
			
			
	}
}
	
?>